package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.repository.TravelMongoRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMongoRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Travel travel) {
        repository.save(travel);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(Travel travel) {
        repository.save(travel);
    }

    @Override
    public Travel find(String id) {
        Travel travel = repository.findById(id).orElse(null);
        if(travel != null){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.find(travel.getDestId()));
        }
        return travel;
    }

    @Override
    public List<Travel> findAll() {
        List<Travel> list = repository.findAll();
        return list;
    }

    @Override
    public Page query(TravelQueryObject qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if(qo.getState() >= 0){
            query.addCriteria(Criteria.where("state").is(qo.getState()));
        }
        //添加范围查询条件
        TravelCondition day = qo.getDay();
        if(day != null){
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }
        TravelCondition perExpend = qo.getPerExpend();
        if(perExpend != null){
            query.addCriteria(Criteria.where("perExpend").gte(perExpend.getMin()).lte(perExpend.getMax()));
        }
        long count = template.count(query, Travel.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable page;
        if(qo.getOrderType() ==1){
            page = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(),Sort.Direction.DESC,"createTime");
        }else{
            page = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(),Sort.Direction.DESC,"viewnum");
        }
        query.with(page);
        List<Travel> list = template.find(query, Travel.class);
        for (Travel travel : list) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }
        return new PageImpl(list,page,count);
    }

    @Override
    public void changeState(Travel travel) {

        //判断游记是否存在
        Optional<Travel> op = repository.findById(travel.getId());
        if(op.isPresent() && travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("参数异常");
        }
        Travel tv = this.find(travel.getId());
        //如果状态为审核通过状态,游记发布时间改为当前时间,最后更新时间改为当前时间
        if(travel.getState() == Travel.STATE_WAITING){
            tv.setState(Travel.STATE_RELEASE);
            tv.setCreateTime(new Date());
            tv.setLastUpdateTime(new Date());
        }else{
            tv.setState(Travel.STATE_REJECT);
            tv.setCreateTime(null);
            tv.setLastUpdateTime(new Date());
        }
        this.update(tv);
    }

    @Override
    public String getContentById(String id) {
        Travel travel = this.find(id);

        return travel.getContent();
    }

    @Override
    public List<Travel> viewnumTop3(String destId) {

        PageRequest of = PageRequest.of(0, 3);
        List<Travel> list = repository.findByDestId(destId,of);

        return list;
    }

    @Override
    public String saveOrUpdate(Travel travel, UserInfo userInfo) {
        travel.setDestName(destinationService.find(travel.getDestId()).getName());
        travel.setUserId(userInfo.getId());
        travel.setLastUpdateTime(new Date());
        if(!StringUtils.hasLength(travel.getId())){
            travel.setId(null);
            travel.setCreateTime(new Date());
            this.save(travel);
        }else{
            Travel tv = this.find(travel.getId());
            travel.setViewnum(tv.getViewnum());
            travel.setReplynum(tv.getReplynum());
            travel.setFavornum(tv.getFavornum());
            travel.setSharenum(tv.getSharenum());
            travel.setThumbsupnum(tv.getThumbsupnum());
            travel.setContent(tv.getContent());
            this.save(travel);

        }
        return null;
    }

    @Override
    public List<Travel> findByDestName(String keyword) {
        List<Travel> list = repository.findByDestName(keyword);
        for (Travel travel : list) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }
        return list;
    }


}
